/*
 * MainApplication.cpp
 *
 *  Created on: Apr 16, 2017
 *      Author: zhangjf
 */

#include <Poco/ThreadPool.h>
#include <Poco/FileChannel.h>
#include <Poco/DateTimeFormatter.h>
#include <Poco/PatternFormatter.h>
#include <Poco/FormattingChannel.h>

using namespace Poco;

#include "Helper.h"
#include "DatabaseSubsystem.h"
#include "NetworkSubsystem.h"
#include "CellSubsystem.h"
#include "MainApplication.h"
#include <MainWorkerTask.h>

using namespace global;
using namespace util;
using namespace network;
using namespace database;
using namespace cell;

MainApplication::MainApplication()
{
	// TODO Auto-generated constructor stub
}

MainApplication::~MainApplication()
{
	// TODO Auto-generated destructor stub
}

//void MainApplication::initLogger(const std::string& strLogName)
//{
//	AutoPtr<FileChannel> pChannel(new FileChannel);
//	pChannel->setProperty("path", strLogName);
//	pChannel->setProperty("rotation", " 20 M");
//	pChannel->setProperty("archive", "timestamp");
//
//	AutoPtr<PatternFormatter> pPF(new PatternFormatter);
//	pPF->setProperty("pattern", "%Y-%m-%d %H:%M:%S %s(%T): %t");
//	pPF->setProperty("times","local");
//	AutoPtr<FormattingChannel> pFC(new FormattingChannel(pPF, pChannel));
//	logger().setChannel(pFC);
//}

void MainApplication::initialize(Application& self)
{
	Application::initialize(self);

	std::string fname = std::string(Application::instance().name()) + ".log";
	Helper::initLogger(logger(), fname);
	//poco_information(logger(), __func__ + string(": ") + fname);
}

void MainApplication::uninitialize()
{
	Application::uninitialize();
}

int MainApplication::main(const std::vector<std::string>& args)
{
	MainWorkerTask mainWorkTask;
	ThreadPool::defaultPool().start(mainWorkTask);

	waitForTerminationRequest();

	mainWorkTask.cancel();

	return Application::EXIT_OK;
}

int main(int argc, char** argv)
{
	Poco::AutoPtr<MainApplication> pApp = new MainApplication;
	pApp->addSubsystem(new DatabaseSubsystem());
	pApp->addSubsystem(new NetworkSubsystem());
	pApp->addSubsystem(new CellSubsystem());

	try
	{
		return pApp->run(argc, argv);
	}
	catch (Poco::Exception& exc)
	{
		pApp->logger().log(string("main::exception::") + exc.displayText());
		return Poco::Util::Application::EXIT_CONFIG;
	}
}
